<template>

    <ul class="list">
        <li class="item"
            v-for="item of letters"
            :key="item"
            @click="handleLetterClick"
            @touchstart.prevent="handTouchStart"
            @touchmove="handTouchMove"
            @touchend="handTouchEnd"
            :ref="item"
        >
            {{item}}
        </li>
    </ul>

</template>

<script>
import { clearTimeout, setTimeout } from 'timers'
export default {
  props: {
    citysList: Object
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: 0
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  computed: {
    letters () {
      const letters = []
      for (let key in this.citysList) {
        letters.push(key)
      }
      return letters
    }
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handTouchStart () {
      this.touchStatus = true
    },
    handTouchMove (e) {
      if (!this.touchStatus) {
        return
      }
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        const touchY = e.touches[0].clientY - 79
        const index = Math.floor((touchY - this.startY) / 20)
        if (index >= 0 && index < this.letters.length) {
          this.$emit('change', this.letters[index])
        }
      }, 16)
    },
    handTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>

<style scoped lang='stylus'>
    @import '~@/assets/styles/varibles.styl';
    .list {
        display : flex
        position : absolute
        flex-direction : column
        top : 1.58rem
        right : 0
        bottom : 0
        background : #eee
        justify-content : center
        width : 0.4rem
        align-items : center
        .item {
            line-height : 0.4rem
            color : $bgColor
        }
    }
</style>
